﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using YookingCrypto.Class;
using YookingFileIO.Class;
using YookingSvrDBCall.Class.DBLogic;
using YookingSvrDBCall.Class.DBPub;

namespace YookingBizWQW.Class.Public
{
    public class clsYkKeyData
    {
        private string strDes64Key = "W^=u2aPs"; //DES加密密钥
        private string strDes64Iv = "2a9i@TaM";  //DES初始向量

        //RSA_JAVA、RSA_JS私钥、公钥明文
        private string strRSAPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzzbD0/AtktCbBp24YVRe9GtsnetUi6MtR6VeVDlK9IL2/aYFy1HJ7W9hpVnUpYpEFTPdmEgABEHBy3pTi3FplGd10DGDJ6PUZaQ4HovWQLUMWVVMp03QMKYot8C+nmUfyl+xhSJJecNOG+T5PGsWkqWsmJnHU24vrAadCLDHaAW1RjJFXPXMvS6DOf+nmTB3MMGAy9HHe12IyUn7CrMx5ukWfsmJB/TqFJcrwlF15MRWHK2Q0OLrzlFeAUtaE40bujPY8aBOpB3EgXhWFKCDWE/PlzN3+/b21G69cXOVKBI9PaqsCMAk5AJ9LSiwfAF+DZKhMc/AB6QB/HWlQBd2owIDAQAB";
        private string strRSAPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDPNsPT8C2S0JsGnbhhVF70a2yd61SLoy1HpV5UOUr0gvb9pgXLUcntb2GlWdSlikQVM92YSAAEQcHLelOLcWmUZ3XQMYMno9RlpDgei9ZAtQxZVUynTdAwpii3wL6eZR/KX7GFIkl5w04b5Pk8axaSpayYmcdTbi+sBp0IsMdoBbVGMkVc9cy9LoM5/6eZMHcwwYDL0cd7XYjJSfsKszHm6RZ+yYkH9OoUlyvCUXXkxFYcrZDQ4uvOUV4BS1oTjRu6M9jxoE6kHcSBeFYUoINYT8+XM3f79vbUbr1xc5UoEj09qqwIwCTkAn0tKLB8AX4NkqExz8AHpAH8daVAF3ajAgMBAAECggEBAJkUvFc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g5lkNeCPALyifAf8RuK6fzvOOEgEqQ3runJouozqlmieSsNKRnnu/YRhPa06LuhqjxCh2EXsgbNt8g9CkSNG692/Ig1pRQt3q9dMKwgSe62DOjVuDrUvkLmQr6LzJDJltzpbbOrDk/rVzQEZ9SrQ==";

        //AES加密密钥
        private string strAes128Key = "LzI3ZVd9N3ky$DJrODg0NiEuNzd^Qz4t";

        //WQW加密密钥及验证码
        private string strWqw16Key = "YzAxN2EyYTU0ZjIz";
        private string strWqw8Code = "72316991";

        #region DES加密、解密

        //注释：王庆文 2020年04月09日==============================================
        //DES加密
        public string DesEncrypt(string strDeContent, string strExtCode)
        {
            if (strExtCode == "8565a40@abb65dd9613cddd4bd8f617fb.cn")
            {
                clsSecret myDes = new clsSecret(); //DES加密的结果
                return myDes.EncryptString(strDeContent, strDes64Key, strDes64Iv);
            }
            else
            {
                return strDeContent; //附加码不正确，则原路返回
            }
        }

        //注释：王庆文 2020年04月09日==============================================
        //DES解密
        public string DesDecrypt(string strEnContent, string strExtCode)
        {
            if (strExtCode == "8565a40@abb65dd9613cddd4bd8f617fb.cn")
            {
                clsSecret myDes = new clsSecret(); //DES解密的结果
                return myDes.DecryptString(strEnContent, strDes64Key, strDes64Iv);
            }
            else
            {
                return strEnContent; //附加码不正确，则原路返回
            }
        }

        #endregion

        #region RSA公钥加密，私钥解密

        //注释：王庆文 2020年04月09日=============================================================================
        //RSA公钥加密
        public string RSAEncrypt(string strDeContent, string strExtCode)
        {
            if (strExtCode == "8565a40@abb65dd9613cddd4bd8f617fb.cn")
            {
                clsRSA_Java myRSA = new clsRSA_Java(RSAType_Java.RSA2, Encoding.UTF8, null, strRSAPublicKey);
                return myRSA.EncryptBigDataJava(strDeContent); //公钥加密的结果
            }
            else
            {
                return strDeContent; //附加码不正确，则原路返回
            }
        }

        //注释：王庆文 2020年04月09日=============================================================================
        //RSA私钥解密
        public string RSADecrypt(string strEnContent, string strExtCode)
        {
            if (strExtCode == "8565a40@abb65dd9613cddd4bd8f617fb.cn")
            {
                clsRSA_Java myRSA = new clsRSA_Java(RSAType_Java.RSA2, Encoding.UTF8, strRSAPrivateKey, null);
                return myRSA.DecryptBigDataJava(strEnContent); //私钥解密的结果
            }
            else
            {
                return strEnContent; //附加码不正确，则原路返回
            }
        }

        #endregion

        #region 查询RSA公钥、私钥的文本串值

        //注释：王庆文 2020年04月09日===============================================================
        //得到RSA公钥、私钥的具体内容
        public string GetRSAKey(string strB64RequestData)
        {
            string strJsonArray = "[{\"PublicKey\":\"null\",\"PrivateKey\":\"null\"}]";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strUserName = "test"; //用户名
            string strEnPassWord = "1234"; //登录密码（BASE64密文）

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strUserName = strArray[0];
                strEnPassWord = strArray[1];
            }

            #endregion

            //检查用户是否有权调阅RSA公钥及RSA私钥
            if (IsExistUser(strUserName, strEnPassWord))
            {
                strJsonArray = "[{";
                strJsonArray += "\"PublicKey\":\"" + strRSAPublicKey + "\",";
                strJsonArray += "\"PrivateKey\":\"" + strRSAPrivateKey + "\"";
                strJsonArray += "}]";
            }

            return strJsonArray; //得到RSA密钥数据
        }

        #endregion

        #region 查询DES密钥及向量的文本串值

        //注释：王庆文 2020年04月09日===============================================================
        //得到DES密码、初始向量的具体内容
        public string GetDESKey(string strB64RequestData)
        {
            string strJsonArray = "[{\"Des64Key\":\"null\",\"Des64Iv\":\"null\"}]";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strUserName = "test"; //用户名
            string strEnPassWord = "1234"; //登录密码（BASE64密文）

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strUserName = strArray[0];
                strEnPassWord = strArray[1];
            }

            #endregion

            //检查用户是否有权调阅RSA公钥及RSA私钥
            if (IsExistUser(strUserName, strEnPassWord))
            {
                strJsonArray = "[{";
                strJsonArray += "\"Des64Key\":\"" + strDes64Key + "\",";
                strJsonArray += "\"Des64Iv\":\"" + strDes64Iv + "\"";
                strJsonArray += "}]";
            }

            return strJsonArray; //得到DES密钥数据
        }

        //注释：王庆文 2020年12月31日===============================================================
        //得到DES密码、初始向量的具体内容，变量名字采用难猜的字符串随便起名
        public string GetDESKey_Temp(string strB64RequestData)
        {
            string strJsonArray = "[{\"YkAsdfk\":\"null\",\"YkAsdfv\":\"null\"}]";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strUserName = "test"; //用户名
            string strEnPassWord = "1234"; //登录密码（BASE64密文）

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strUserName = strArray[0];
                strEnPassWord = strArray[1];
            }

            #endregion

            //检查用户是否有权调阅RSA公钥及RSA私钥
            if (IsExistUser(strUserName, strEnPassWord))
            {
                strJsonArray = "[{";
                strJsonArray += "\"YkAsdfk\":\"" + strDes64Key + "\",";
                strJsonArray += "\"YkAsdfv\":\"" + strDes64Iv + "\"";
                strJsonArray += "}]";
            }

            return strJsonArray; //得到DES密钥数据
        }

        #endregion

        #region 查询AES密钥的文本串值

        //注释：王庆文 2021年05月22日===============================================================
        //得到AES密码的具体内容，变量名字采用难猜的字符串随便起名
        public string GetAESKey_Temp(string strB64RequestData)
        {
            string strJsonArray = "[{\"A905bsdrUV82R\":\"null\"}]";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strUserName = "test"; //用户名
            string strEnPassWord = "1234"; //登录密码（BASE64密文）

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strUserName = strArray[0];
                strEnPassWord = strArray[1];
            }

            #endregion

            //检查用户是否有权调阅RSA公钥及RSA私钥
            if (IsExistUser(strUserName, strEnPassWord))
            {
                strJsonArray = "[{";
                strJsonArray += "\"A905bsdrUV82R\":\"" + strAes128Key + "\"";
                strJsonArray += "}]";
            }

            return strJsonArray; //得到AES密钥数据
        }

        #endregion

        #region 查询WQW密钥及验证码的文本串值

        //注释：王庆文 2021年05月26日===============================================================
        //得到WQW密码、验证码的具体内容，变量名字采用难猜的字符串随便起名
        public string GetWQWKey_Temp(string strB64RequestData)
        {
            string strJsonArray = "[{\"Q307budrKT63P\":\"null\",\"Q243haluYW92R\":\"null\"}]";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strUserName = "test"; //用户名
            string strEnPassWord = "1234"; //登录密码（BASE64密文）

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strUserName = strArray[0];
                strEnPassWord = strArray[1];
            }

            #endregion

            //检查用户是否有权调阅WQW加密算法的密钥信息
            if (IsExistUser(strUserName, strEnPassWord))
            {
                strJsonArray = "[{";
                strJsonArray += "\"Q307budrKT63P\":\"" + strWqw16Key + "\",";
                strJsonArray += "\"Q243haluYW92R\":\"" + strWqw8Code + "\"";
                strJsonArray += "}]";
            }

            return strJsonArray; //得到WQW密钥数据
        }

        #endregion

        #region 检查发出解密请求的用户是否存在，密钥是否正确

        //注释：王庆文 2020年04月09日===============================================================
        //判断指定用户名、密码的用户是否存在
        private bool IsExistUser(string strUserName, string strEnPassWord)
        {
            //将BASE64密码转成明文字符串
            string strDePassWord = clsBase64.Base64Decode(strEnPassWord);

            clsSecret myDes = new clsSecret(); //进行DES专用加密
            strEnPassWord = myDes.EncryptString(strDePassWord);

            string strQuerySQL = " SELECT USERNAME FROM SA_USR_LOGININFO ";
            strQuerySQL += " WHERE " + clsSQLSyntax.IsNullSQL("Data", "SYS_CANCELDATETIME");
            strQuerySQL += " AND USERNAME = '" + strUserName + "' ";
            strQuerySQL += " AND PASSWORD = '" + strEnPassWord + "' ";

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            DataSet dsUser = myDB.YookingExecuteDataSet("Data", strQuerySQL);
            if (dsUser != null && dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
            {
                return true; //找到了符合条件的用户
            }
            else
            {
                return false; //没有符合条件的用户
            }
        }

        #endregion


    }
}
